﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcContrib.Pagination;
using Project.DAL.Repository;
using Project.Domain.Entities;

namespace Project.WebUI.Areas.Admin.Controllers
{
    public class CommonController : Controller
    {      
        #region "private"        
        private readonly ICommonRepository _commonRepository;
        public CommonController(ICommonRepository commonRepository)
        {
            _commonRepository = commonRepository;
        }
        #endregion    
 
        #region LoadAjax
        public ActionResult LoadAjax(int page)
        {
            var listAllCommon = _commonRepository.GetAll();
            ViewBag.result = listAllCommon.Count();
            ViewBag.page = page;
            if (page == 1)
            {
                ViewBag.start = 0;
            }
            else
            {
                ViewBag.start = (page - 1) * 10;
            }
            return PartialView("_MainContent", listAllCommon.AsPagination(page, 10));
        }
        #endregion

        public ActionResult Index()
        {
            return View();
        }

        #region Modify Obj
        public ActionResult Modify(int? id)
        {
            if (Session["admin"] == null)
                return RedirectToAction("Login", "DashBoard");
            if (id != null)
            {
                var obj = _commonRepository.GetById((int)id);
                ViewBag.Id = id;
                return View(obj);
            }            
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]        
        public ActionResult Modify(int? id, FormCollection obj)
        {
            var contentVi = obj["content_vi"];
            var contentEn = obj["content_En"];
            var type = obj["type"];
            if (id != null)
            {
                var common = _commonRepository.GetById((int)id);
                if (common != null)
                {
                    common.Content = contentVi;
                    common.ContentEn = contentEn;
                    common.Type = type;
                    _commonRepository.SaveChanges();
                    return Content("1");
                }
                return Content("0");
            }
            else
            {
                var common = new Common()
                    {
                        Content = contentVi,
                        ContentEn = contentEn,
                        Type = type,
                        Status = true,
                        CreatedTime = DateTime.Now
                    };
                
                try
                {
                    _commonRepository.Add(common);
                    _commonRepository.SaveChanges();
                    //sucessfull
                    return Content("1");
                }
                catch (Exception ex)
                {
                    //Error
                    return Content("0");
                }
            }
        }
        #endregion

        #region "UpdateStatus"
        public ActionResult UpdateStatus(int id, int status)
        {
            var mach = _commonRepository.GetById(id);
            if (status == 1)
            {
                mach.Status = false;
                _commonRepository.SaveChanges();
                ViewBag.status = false;
                ViewBag.id = id;
                ViewBag.controller = "Common";
                return PartialView("_UpdateStatus");
            }
            mach.Status = true;
            _commonRepository.SaveChanges();
            ViewBag.status = true;
            ViewBag.id = id;
            ViewBag.controller = "Common";
            return PartialView("_UpdateStatus");
        }

        #endregion
        
    }
}
